package com.wyq.miaosha.validator;

import com.wyq.miaosha.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {

    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required(); //当前注解是否开启
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(required) { //注解开启了
            return ValidatorUtil.isMobile(s);
        }else {
            if(StringUtils.isEmpty(s)) {  //没开启 判断是否为空即可
                return false;
            }else {
                return true;
            }
        }
    }
}
